package com.copro.chemdb.result;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.ihelpuoo.lang.NumericUtils;

import com.copro.simple.Application;
import com.copro.simple.SimpleException;

public class DeleteResultAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		try {
			int resultId = NumericUtils
					.parseInt(request.getParameter("id"), -1);
			if (resultId <= 0)
				throw new SimpleException("You can not delete result by id "
						+ resultId);
			Application app = Application.me(request);
			String clearAnswer, delResult;

			clearAnswer = app.getSQL("clearAnswer").set("resultId", resultId)
					.toString();
			delResult = app.getSQL("deleteResult").set("id", resultId)
					.toString();

			app.execBatch(clearAnswer, delResult);
		} catch (SimpleException e) {
			request.setAttribute("error", e.getMessage());
			return mapping.findForward("del-failed");
		}
		return mapping.findForward("del-ok");
	}
}
